package com.guigu.financial.controller;

import com.financal.util.Result;
import com.financal.util.StatusCode;
import com.github.pagehelper.PageInfo;
import com.guigu.financial.pojo.BorrowInfo;
import com.guigu.financial.pojo.vo.BorrowInfoApprovalVO;
import com.guigu.financial.service.BorrowInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@RequestMapping("/borrowInfo")
public class BorrowInfoController {
    @Autowired
    private BorrowInfoService borrowInfoService;

//    @GetMapping("")
//    public List<BorrowInfo> findByBorrower(){
//       return borrowInfoService.findAll();
//    }

    @PostMapping("/approval")
    public Result approval(@RequestBody BorrowInfoApprovalVO borrowInfoApprovalVO) {
        borrowInfoService.approval(borrowInfoApprovalVO);
        return new Result(true, StatusCode.OK,"审核成功！");
    }

    @GetMapping("")
    public PageInfo pageBorrowInfo(@RequestParam(required = false) int pageNum, int limit){
        PageInfo pageBorrowInfo  = borrowInfoService.findAll(pageNum, limit);
        return pageBorrowInfo;
    }

    // 保存borrower到ServletContext中
    @PostMapping("")
    public void saveBorrowerInServletContext(@RequestBody BorrowInfo borrowInfo, HttpServletRequest request){
        request.getServletContext().setAttribute("borrowInfo",borrowInfo);
    }

    @GetMapping("getBorrowInfo")
    public BorrowInfo findDetail(HttpServletRequest request){
        BorrowInfo borrowInfo = (BorrowInfo) request.getServletContext().getAttribute("borrowInfo");
        return borrowInfo;
    }
}
